import {Component, OnInit} from "@angular/core";
import {Hero} from "./Hero";
import {HeroService} from "./hero.service";
import {ActivatedRoute, Params, Router} from "@angular/router";

@Component({
  selector: 'my-hero-detail',
  templateUrl: 'app/hero-detail.component.html',
  styleUrls: ['app/hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {
  hero: Hero

  constructor(private heroService: HeroService,
              private route: ActivatedRoute,
              private router: Router) {
  }

  goBack() {
    // window.history.back()
    let heroId = this.hero ? this.hero.id : null
    this.router.navigate(['/heroes', {id: heroId, foo: 'foo'}])
  }

  ngOnInit() {
    this.route.params.forEach((params: Params) => {
      let id = +params['id']
      this.heroService.getHero(id).then(hero => this.hero = hero)
    })
  }
}